package de.medizin.uni.halle.irm;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class TextSettingsFragment extends SherlockFragment{

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    	final View v = inflater.inflate(R.layout.textsettings, container, false);
    	final SharedPreferences prefs = getSherlockActivity().getSharedPreferences("emailpreferences", Context.MODE_PRIVATE);
    	((EditText) v.findViewById(R.id.emailcontent)).setText(prefs.getString("emailcontent", ""));
    	((EditText) v.findViewById(R.id.emailsubject)).setText(prefs.getString("emailsubject", ""));
		int count = 170 - ((EditText) v.findViewById(R.id.smscontent)).getText().length();
		((EditText) v.findViewById(R.id.smscount)).setText(String.valueOf(count));			
    	((EditText) v.findViewById(R.id.smscontent)).addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable arg0) {
				int count = 170 - ((EditText) v.findViewById(R.id.smscontent)).getText().length();
				((EditText) v.findViewById(R.id.smscount)).setText(String.valueOf(count));				
			}
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}    		
    	});
    	
    	((Button) v.findViewById(R.id.save)).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
		    	SharedPreferences.Editor editor = prefs.edit();
		    	editor.putString("emailcontent", ((EditText) v.findViewById(R.id.emailcontent)).getText().toString());
		    	editor.putString("emailsubject", ((EditText) v.findViewById(R.id.emailsubject)).getText().toString());
		    	editor.commit();
	    		Toast.makeText(getSherlockActivity(),"Einstellungen gespeichert", Toast.LENGTH_SHORT).show();
			}
		});    	
        return v;
    }
}
